<template>
	<view class="home-layout page-bg">
		<custom-nav-bar>推荐</custom-nav-bar>
		<!-- 顶部banner -->
		<view class="banner">
			<swiper indicator-dots circular :autoplay="true" indicator-color="rgba(255,255,255,0.5)"
				indicator-active-color="#fff" :interval="3000" :duration="1000">
				<swiper-item v-for="item in bannerList" :key="item._id">
					<navigator v-if="item.target=='self'" class="link" :url="`/pages/classlist/classlist?${item.url}`">
						<image :src="item.picurl" mode="aspectFill" />
					</navigator>
					<!-- <navigator v-else  class="link" :app-id="item.appid" :url="item.url">
							<image :src="item.picurl" mode="aspectFill" />
					</navigator> -->
					<!-- 跳转到其他小程序 -->
					<view v-else class="link" @click="handleJumpMiniP(item)">
						<image :src="item.picurl" mode="aspectFill" />
					</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 公告 -->
		<view class="notice">
			<view class="left">
				<!-- 通知的图标 -->
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical autoplay interval="2000" duration="300" circular>
					<swiper-item v-for="notice in noticeList" :key="notice._id">
						<navigator :url="`/pages/notice/notice?id=${notice._id}`">
							{{notice.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="18" color="#999"></uni-icons>
			</view>
		</view>
		<!-- 推荐 -->
		<view class="select">
			<common-title>
				<template #name>每日推荐</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="20"></uni-icons>
						<uni-dateformat :date="Date.now()" format="dd号"></uni-dateformat>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x>
					<image v-for="(item, index) in randomWall" @click="handlePreview(index)" :key="item._id"
						:src="item.smallPicurl" mode="aspectFill" />
				</scroll-view>
			</view>
		</view>
		<!-- 专题 -->
		<view class="theme">
			<common-title>
				<template #name>专题精选</template>
				<template #custom>
					<navigator class="more">More+</navigator>
				</template>
			</common-title>

			<view class="content">
				<theme-item v-for="item in classifyList" :key="item._id" :classify="item"></theme-item>
				<theme-item :isMore="1"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue'
	import {
		reqBannnerList,
		reqRandomWall,
		reqWallNewsList
	} from '@/apis/index.js'
	import {
		reqIndexClassify
	} from '@/apis/classify.js'
	import useClassifyStore from '@/pinia/modules/classify.js'
	import {
		onShareAppMessage,
		onShareTimeline
	} from '@dcloudio/uni-app'

	let bannerList = ref([])
	let randomWall = ref([])
	let noticeList = ref([])
	let classifyList = ref([])
	const classifyStore = useClassifyStore()
	onMounted(() => {
		// 获取 banner列表
		initBannerList()
		// 获取每日推荐
		initRandomWall()
		// 公告
		initWallNewsList()
		// 专题精选
		initClassifyList()
	})

	const handleJumpMiniP = item => {
		uni.navigateToMiniProgram({
			appId: item.appid,
			path: item.path,
			success(res) {
				console.log('跳转成功', res);
			},
			fail(err) {
				console.error('跳转失败', err);
			}
		})
	}

	const handlePreview = (index) => {
		classifyStore.setClassifyList(randomWall.value)
		// 跳转
		uni.navigateTo({
			url: `/pages/preview/preview?index=${index+1}`
		})
	}

	const initClassifyList = async () => {
		const {
			data
		} = await reqIndexClassify({
			select: false
		})
		classifyList.value = data
	}

	const initWallNewsList = async () => {
		const {
			data
		} = await reqWallNewsList({
			select: true,
			pageNum: 1,
			pageSize: 10
		})
		noticeList.value = data
	}

	const initRandomWall = async () => {
		const {
			data
		} = await reqRandomWall()
		randomWall.value = data
	}

	const initBannerList = async () => {
		const {
			data
		} = await reqBannnerList()
		bannerList.value = data
	}

	// 分享给朋友 
	onShareAppMessage(e => {
		return {
			title: '~咸虾米壁纸~'
		}
	})

	// 朋友圈
	onShareTimeline(() => {
		return {
			title: '~咸虾米壁纸~',
			imageUrl: '/static/xxmLogo.png'
		}
	})
</script>

<style lang="scss" scoped>
	.home-layout {
		.banner {
			width: 750rpx;
			padding: 30rpx 0;

			swiper {
				width: 100%;
				height: 340rpx;

				&-item {
					width: 100%;
					height: 100%;
					padding: 0 30rpx;

					.link {
						width: 100%;
						height: 100%;

						image {
							width: 100%;
							height: 100%;
							border-radius: 4px;
						}
					}
				}
			}
		}

		.notice {
			width: 690rpx;
			margin: 0 auto;
			padding: 0 20rpx;
			display: flex;
			align-items: center;
			border-radius: 40rpx;
			height: 70rpx;
			background-color: #efefef;

			.left {
				width: 120rpx;
				display: flex;
				align-items: center;

				:deep(.uni-icons) {
					color: $brand-theme-color !important;
				}

				// .uni-icons{
				// 	color: $brand-theme-color !important;
				// }
				text {
					font-size: 28rpx;
					margin-left: 5rpx;
					color: $brand-theme-color;
					font-weight: bold;
				}
			}

			swiper {
				height: 36rpx;
				width: 500rpx;
				font-size: 28rpx;

				&-item {
					width: 100%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}

		.select {
			padding-top: 30rpx;

			.date {
				display: flex;
				align-items: center;
				color: $brand-theme-color;

				:deep(.uni-icons) {
					color: $brand-theme-color !important;
				}

				uni-dateformat {
					display: inline-block;
					margin-left: 8rpx;
					font-size: 28rpx;
				}
			}

			.content {
				width: 720rpx;
				padding-right: 20rpx;
				margin-left: 30rpx;
				margin-top: 30rpx;

				scroll-view {
					white-space: nowrap;

					image {
						width: 200rpx;
						margin-right: 10px;
						border-radius: 10rpx;
						display: inline-block;
					}

				}
			}
		}

		.theme {
			padding: 50rpx 0;

			.more {
				font-size: 32rpx;
				color: #999;
			}

			.content {
				margin-top: 30rpx;
				padding: 0 30rpx;
				display: grid;
				grid-template-columns: repeat(3, 1fr);
				gap: 20rpx;
			}
		}
	}
</style>